





//
//  FilterViewController.m
//  nncloud
//
//  Created by macfeel on 2019/5/19.
//  Copyright © 2019 macfai. All rights reserved.
//

#import "FilterViewController.h"
#import "FilterCell.h"
#import "EquipmentListViewController.h"
#import "FacturerListViewController.h"
#import "EditToFillViewController.h"
#import "GuiGeXinghaoViewController.h"
#import "FilterResultViewController.h"
//
@interface FilterViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *nameArray;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger currentPage;

@property (nonatomic,strong)NSArray *titleArray;

@property (nonatomic,strong)NSMutableArray *dictArray;

//@property (nonatomic,strong)NSMutableArray *equipArray;

@property (nonatomic,assign)NSInteger currentRow;

///接收字典
@property(nonatomic,strong)NSDictionary *dict;

//
//@property(nonatomic,strong)NSArray *rightArray;
@property(nonatomic,assign)BOOL isDate;

///标记器材种类
@property(nonatomic,assign)BOOL isQicaiType;

///标记设备型号
@property(nonatomic,assign)BOOL isShebeiXinghao;

///标记厂家
@property(nonatomic,assign)BOOL isFactory;

///日期字符串
@property(nonatomic,copy)NSString *dateString;

///器材字符串
@property(nonatomic,copy)NSString *qicaiString;

///厂家字符串
@property(nonatomic,copy)NSString *factrotyString;

///设备型号字符串
@property(nonatomic,copy)NSString *shebeiXinghaoString;


///编辑的填写的内容
@property(nonatomic,copy)NSString *content;

@end





@implementation FilterViewController

#define cellIdentifier  @"relayd"
//#define cellIdentifier  @"filter"
//#define HeaderHight  290
#define PageSize 20

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - ScreenHeightFlex) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 55;
        //        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
//        [_tableView registerNib:[UINib nibWithNibName:@"FilterCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
        //        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //            [ws loadData];
        //        }];
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setItemWithTitle:@"筛选" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    ///数组没有初始化 会导致一些奇怪的问题  比如数组 明明往里面加东西了还是空的，或者tableView代理方法不走
    self.dataArray = [NSMutableArray array];
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 35, 35) title:@"确认筛选" Image:nil selectedIMG:nil tartget:self action:@selector(FilterAction:)];
    
    
//    self.nameArray = @[@"段编号",@"器材种类",@"型号",@"生产厂家",@"周期",@"组合位置",@"入所次数",@"返所原因",@"验收次数",@"出厂日期",@"入所日期",@"检修日期",@"验收日期",@"出所日期",@"到期日期",@"预检员",@"检验员",@"验收员",@"厂编号",@"站名",@"报废日期",@"测试数据表编号",@"使用处所",@"备注",];
    
    [self.view addSubview:self.tableView];
    self.isDate = NO;
    self.isQicaiType = NO;
    self.isFactory = NO;
    ///接收上个页面传过来的字典进行数据展示
    [self formData];
    // Do any additional setup after loading the view from its nib.
}


///进行数据处理
-(void)formData{
    
    NSDictionary *dict;
    if (self.model) {
        ///传过来的模型转成字典
        dict = [self.model mj_keyValues];
        YYLog(@"为什么这么少heheh:%@",dict);
        self.dict = dict;
        YYLog(@"%@",dict);
    }
    
    NSArray *dictArray = [self.dict allKeys];
//    self.titleArray = [dictArray copy];
    YYLog(@"%lu",(unsigned long)self.titleArray.count);
    
    ////数组遍历删除  规则：遍历
    ///遍历删除 时候  需要进行  深拷贝
    NSMutableArray *arr = [dictArray mutableCopy];
    NSMutableArray *tempArr = [arr mutableCopy];
    for (NSString *objString in tempArr) {
        if ([objString containsString:@"日期"]) {
            [arr removeObject:objString];
        }
    }
    ////因为筛选暂时不要日期
    self.titleArray = [arr copy];
    YYLog(@"%@",self.titleArray);
    //NSDictionary *dict = self.dict;
    for (int i=0; i<self.titleArray.count; i++) {
        
        NSString *key = self.titleArray[i];
        
        NSString *value = [dict objectForKey:key];
        //处理null
//        if ([value isKindOfClass:[NSNull class]]) {
//            value = @"";
//        }
        //处理返回的int 类型  因为赋值时候需要进行字符串赋值
//        if ([value isKindOfClass:[NSNumber class]]) {
//            value = [NSString stringWithFormat:@"%@",value];
//        }
        
        if (value.length>0) {
            value = @"";
        }
        [self.dataArray addObject:value];
    }
    
//    YYLog(@"%@",self.dataArray);
//    YYLog(@"%lu",(unsigned long)self.dataArray.count);
    [self.tableView reloadData];
}


#pragma mark - 确认筛选
//点击
- (void)FilterAction:(UIButton *)btn{
    
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    for (int i=0; i<self.titleArray.count; i++) {
        [dic setObject:self.dataArray[i] forKey:self.titleArray[i]];
    }
    
    ///点击了筛选
    [NUserModel shareInstance].isFilter = YES;
    FilterResultViewController *filter = [FilterResultViewController new];
    filter.filterJsonString = [dic mj_JSONString];
    [self.navigationController pushViewController:filter animated:false];
    
    ///method  1 修改    3新增    2复制添加
//    NSDictionary *dict = @{@"data":[dic mj_JSONString],@"method":@"3"};
    
    ///对应的是 data 传到首页进行筛选
    
//    [NUserModel shareInstance].filterJsonString = [dic mj_JSONString];
    
//    [NUserModel shareInstance].isFilter = YES;
//    [self.navigationController popViewControllerAnimated:false];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    return self.dataArray.count;
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: cellIdentifier];
    }
    cell.selectionStyle = 0;
    cell.textLabel.textColor = kDarkGrayColor;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //    cell.textLabel.text = self.nameArray[indexPath.row];
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.detailTextLabel.text = self.dataArray[indexPath.row];
    ///筛选cell 暂时换成原生的
//    FilterCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
//    cell.selectionStyle = 0;
//    cell.nameLab.text = self.nameArray[indexPath.row];
//    cell.textFd.delegate = self;
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ///点击哪一行进去，还要记录当前的索引，返回的时候刷新刚才点击的那一行
    self.currentRow = indexPath.row;
    ///点击进去进行编辑
    NSString *titleString = self.titleArray[indexPath.row];
//    if ([titleString containsString:@"日期"]) {
//
//        [UICustomDatePicker showCustomDatePickerAtView:self.view choosedDateBlock:^(NSDate *date) {
//            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//            fmt.dateFormat = @"yyyy-MM-dd";
//            NSString *string = [fmt stringFromDate:date];
//            YYLog(@"current Date:%@",string);
//            ///将选择的时间放到数据源中
//            self.dateString = string;
//            self.isDate = YES;
//            [self updateDateToDataSource];
//
//        } cancelBlock:^{
//
//        }];
//
//    }else
    
         if([titleString containsString:@"器材种类"]||[titleString containsString:@"设备种类"]){
        
        ///继电器 叫器材种类   转辙机 叫设备种类
        //进入继电器类型列表
        self.isQicaiType = YES;
        EquipmentListViewController *equip = [EquipmentListViewController new];
        ///拿到器材种类
        equip.block = ^(NSString * _Nonnull type) {
            YYLog(@"%@",type);
            self.qicaiString = type;
            [self updateDataSource];
            self.isQicaiType = false;
        };
        [self.navigationController pushViewController:equip animated:YES];
        
    }else if([titleString containsString:@"厂家"]){
        //进入继电器类型列表
        self.isFactory = YES;
        FacturerListViewController *equip = [FacturerListViewController new];
        equip.block = ^(NSString * _Nonnull name) {
            YYLog(@"%@",name);
            self.factrotyString = name;
            [self updateDataSource];
            self.isFactory = false;
        };
        [self.navigationController pushViewController:equip animated:YES];
        
    }else if([titleString containsString:@"型号"]){
        ///专门针对继电器来说
        //进入继电器类型列表  继电器叫型号  转辙机叫 设备型号
        self.isShebeiXinghao = YES;
        GuiGeXinghaoViewController *equip = [GuiGeXinghaoViewController new];
        equip.block = ^(NSString * _Nonnull xinghao) {
            self.shebeiXinghaoString = xinghao;
            [self updateDataSource];
            self.isShebeiXinghao = false;
        };
        [self.navigationController pushViewController:equip animated:YES];
        
    }else{
        
        ///输入框进行编辑 各个页面共用
        EditToFillViewController *fill = [EditToFillViewController new];
        fill.block = ^(NSString * _Nonnull content) {
            YYLog(@"%@",content);
            self.content = content;
            [self updateDataSource];
        };
        [self.navigationController pushViewController:fill animated:YES];
    }
}

///将修改后的数据更新到数据源数组中
-(void)updateDataSource{
    
    ////注意 对于不可变数组必须是 mutableCopy 深拷贝 如果是copy 就直接挂掉了
    NSMutableArray *newArray = [self.dataArray mutableCopy];
    YYLog(@"hehe:%@****hehe:%@",self.dataArray,newArray);
    if (self.isDate==YES) {
        ///选择日期
        [newArray replaceObjectAtIndex:self.currentRow withObject:self.dateString?:@""];
        
    }else if (self.isQicaiType==YES){
        ///器材种类  是针对继电器来说的
        [newArray replaceObjectAtIndex:self.currentRow withObject:self.qicaiString?:@""];
        
    }else if (self.isShebeiXinghao==YES){
        
        ///选择设备型号过来的
        [newArray replaceObjectAtIndex:self.currentRow withObject:self.shebeiXinghaoString?:@""];
        
    }else if (self.isFactory==YES){
        
        ///选择厂家过来的
        [newArray replaceObjectAtIndex:self.currentRow withObject:self.factrotyString?:@""];
        
    }else{
        
        ///是从编辑填写页面进来后走的分之
        [newArray replaceObjectAtIndex:self.currentRow withObject:self.content?:@""];
    
    }
    
    self.dataArray = newArray;
    YYLog(@"%@",self.dataArray);
    [self.tableView reloadData];
    ////刷新某一行
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:self.currentRow inSection:0];
    YYLog(@"当前行数：%ld",(long)self.currentRow);
    //组数中的cell
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
}

@end
